package com.qf.quduba.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.quduba.VO.ResultVO;
import com.qf.quduba.entity.Books;
import com.qf.quduba.entity.Chapter;
import com.qf.quduba.service.BookInfoService;
import com.qf.quduba.service.ChapterService;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 作者：程意
 * 时间：2020/10/19 11:24
 */
@RestController
@CrossOrigin
@RequestMapping("/chapter")
public class ChapterController {
    @Resource
    private ChapterService chapterService;
    @Resource
    private BookInfoService bookInfoService;
    @Resource
    private RestHighLevelClient restHighLevelClient;
    @GetMapping("findChapterOne")
    public ResultVO findChapterOne(Integer chapterId){
        Chapter chapter = chapterService.findChapterOne(chapterId);
        if(chapter != null){
            return new ResultVO(1000,"成功",chapter);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }

    @GetMapping("getAllChapter")
    public ResultVO getAllChapter(Integer bookId){
        List<Chapter> chapters = chapterService.getAllChapter(bookId);
        if(chapters != null && chapters.size() > 0){
            return new ResultVO(1000,"成功",chapters);
        }else{
            return new ResultVO(1001,"失败",null);
        }
    }

    @PostMapping("addChapter")
    public ResultVO insertSelective(@RequestBody Chapter chapter){
        try {
            chapter.setChapterTime(new Date());
            int i = chapterService.insertSelective(chapter);
            Books bookInfo = bookInfoService.findBookInfo(chapter.getBookId() + "");
            ObjectMapper objectMapper = new ObjectMapper();
            IndexRequest request = new IndexRequest("quduba");
            String jsonStr = objectMapper.writeValueAsString(bookInfo);
            request.id(bookInfo.getBookId()+"");
            request.source(jsonStr, XContentType.JSON);
            if(i > 0){
                return new ResultVO(1000,"成功",null);
            }else{
                return new ResultVO(1001,"失败",null);
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
    @PostMapping("updateChapter")
    public ResultVO updateChappter(@RequestBody Chapter chapter){
        try {
            int updatachapter = chapterService.updatechapter(chapter);
            Books bookInfo = bookInfoService.findBookInfo(chapter.getBookId() + "");
            ObjectMapper objectMapper = new ObjectMapper();
            IndexRequest request = new IndexRequest("quduba");
            String jsonStr = objectMapper.writeValueAsString(bookInfo);
            request.id(bookInfo.getBookId()+"");
            request.source(jsonStr, XContentType.JSON);
            if (updatachapter > 0){
                return new ResultVO(1000,"成功",null);
            }else{
                return new ResultVO(1001,"失败",null);
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;

    }



}
